package org.yxj.customlibrary.v;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;


import org.yxj.customlibrary.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 字体列表
 *
 * @author yxj on 2022/8/3
 */
public class FontListView extends RecyclerView {

    private List<TypefaceBean> list = new ArrayList<>() ;

    private FontListAdapter adapter ;

    public FontListView(@NonNull Context context) {
        this(context,null);
    }

    public FontListView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FontListView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(HORIZONTAL);
        setLayoutManager(linearLayoutManager);
        addItemDecoration(new MyItem(5));
        setAdapter(adapter = new FontListAdapter(getFontList()));

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                TypefaceBean item = list.get(position);
                check = item.fontName ;

                if (checkFontListener!=null)
                    checkFontListener.onCheckFont(item.typeface);

                adapter.notifyDataSetChanged();
            }
        });
    }


    public List<TypefaceBean> getFontList(){
        list.add(new TypefaceBean(Typeface.createFromAsset(getContext().getAssets(),"zh_1.ttf"),"流线型"));
        list.add(new TypefaceBean(Typeface.createFromAsset(getContext().getAssets(),"zh_2.ttf"),"雅字型"));
        list.add(new TypefaceBean(Typeface.createFromAsset(getContext().getAssets(),"zh_3.ttf"),"端庄型"));
        list.add(new TypefaceBean(Typeface.createFromAsset(getContext().getAssets(),"zh_4.ttf"),"活跃型"));
        list.add(new TypefaceBean(Typeface.createFromAsset(getContext().getAssets(),"zh_5.ttf"),"清秀型"));
        check = "流线型" ;
        return list ;
    }



    public class FontListAdapter extends BaseQuickAdapter<TypefaceBean, BaseViewHolder>{

        public FontListAdapter(@Nullable List<TypefaceBean> data) {
            super(R.layout.text_layout, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, TypefaceBean item) {
            helper.setText(R.id.text,item.fontName) ;
            TextView text =helper.getView(R.id.text) ;
            text.setTypeface(item.typeface);
            if (!TextUtils.isEmpty(check)&&check.equals(item.fontName)){
                text.setBackground(getBG(Color.RED,2));
            }else {
                text.setBackground(getBG(Color.TRANSPARENT,2));
            }
        }
    }



    String check;


    public class TypefaceBean {
        public Typeface typeface ;
        public String fontName ;

        public TypefaceBean( Typeface typeface,String fontName ){
            this.typeface=typeface;
            this.fontName=fontName;
        }

        public TypefaceBean() {
        }
    }

    class MyItem extends ItemDecoration {

        private int span = dp2Px(2);
        public MyItem(int span) {
            this.span = dp2Px(span);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.set(span, span, span, span);
        }
    }

    public int dp2Px(int dp) {
        return (int) (getResources().getDisplayMetrics().density * dp + 0.5f);
    }


    public GradientDrawable getBG(int color, int strokeWidth) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setCornerRadius(40);
        gradientDrawable.setColor(color);
        gradientDrawable.setStroke(strokeWidth, Color.WHITE);
        return gradientDrawable;
    }


    private CheckFontListener checkFontListener ;

    public void setCheckFontListener(CheckFontListener checkFontListener) {
        this.checkFontListener = checkFontListener;
        if (list!=null&&list.size()>0){
            checkFontListener.onCheckFont(list.get(0).typeface);
        }
    }

    public interface CheckFontListener{
        void onCheckFont(Typeface typeface);
    }
}
